#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc,char *argv[])
{
    if(argc!=2)exit(1);
    //printf("stdin:%d\n",stdin->_fileno);
    int fd=open(argv[1],O_RDONLY);
    if(fd<1) exit(1);

    dup2(fd,0);//newfd,oldfd。oldfd是newfd的拷贝
    close(fd);
    while(1)
    {
        char buffer[64];
        if(!fgets(buffer,sizeof(buffer),stdin))break;

        printf("%s",buffer);
    }

    int fd1=open("log1.txt",O_CREAT|O_WRONLY|O_TRUNC,0666);
    if(fd<0) exit(1);
    printf("fd:%d/n",fd);
    while(1)
    {
        char buffer[64];
        int n=read(fd,buffer,sizeof(buffer)-1);
        if(n>0)//读到的字节数
        {
            buffer[n]=0;//内存中手动截断
            printf("%s",buffer);
        }
        else if(n==0)//读到0写端关闭
        break;
    }
    const char*msg ="abcd";
    int cnt=1;
    int a=1234567;
    while(cnt)
    {
        char buffer[16];
        snprintf(buffer,sizeof(buffer),"%d",a);
        write(fd,buffer,strlen(buffer));
        cnt--;
    }
    close(fd);
    return 0;
}
